package RSA;

import java.math.BigInteger;


public class Cipher {
	
	public Cipher()
	{
		
	}

	public static BigInteger[] Encrypt(BigInteger[] tab, Key k)
	{
		/*
		BigInteger tabBig = new BigInteger(tab);
		*/
		BigInteger[] tab2 = new BigInteger[tab.length];
		//System.out.println("Avant cryptage :");
		for(int i=0;i<tab.length;i++)
		{
			tab2[i] = tab[i];
			//System.out.print(tab2[i]+" ");
		}
		//System.out.println("e="+k.e+" n="+k.n);
		//System.out.println("Après cryptage :");
		for(int i=0;i<tab2.length;i++)
		{
			tab2[i] = tab2[i].modPow(k.getExponent(), k.getModulus());
			//System.out.print(tab2[i]+"("+tab2[i].bitLength()+") ");
		}
		/*
		System.out.println();
		byte[] b = RSA.getBytesFromBigIntegerArray(tab2);
		for(int i=0;i<b.length;i++)
		{
			System.out.print(b[i]+" ");
		}
		System.out.println();
		BigInteger[] ee = RSA.getBigIntegerArrayFromBytes(b);
		for(int i=0;i<ee.length;i++)
		{
			System.out.print(ee[i]+" ");
		}
		System.out.println();
		*/
		return tab2;
	}
	
	public static BigInteger[] Decrypt(BigInteger[] tab, Key k)
	{
		//System.out.println("Décrypte");
		return Encrypt(tab, k);
	}
	
	public static BigInteger EncryptToBigInt(BigInteger b, Key k)
	{
		return b.modPow(k.getExponent(), k.getModulus());
	}
	
	public static BigInteger DecryptToBigInt(BigInteger b, Key k)
	{
		return b.modPow(k.getExponent(), k.getModulus());
	}
	
	public static byte[] Encrypt(byte[] tab, Key k)
	{
		//return RSA.getBytesFromBigIntegerArray(Encrypt(RSA.getBigIntegerArrayFromBytes(tab),k));
		/*
		System.out.println(tab.length);
		System.out.println(RSA.getBigIntegerFromBytes(tab));
		System.out.println(EncryptToBigInt(RSA.getBigIntegerFromBytes(tab), k));
		System.out.println(RSA.getBytesFromBigInteger(EncryptToBigInt(RSA.getBigIntegerFromBytes(tab), k)).length);
		*/
		return RSA.getBytesFromBigInteger(EncryptToBigInt(RSA.getBigIntegerFromBytes(tab), k));
	}
	
	public static byte[] Decrypt(byte[] tab, Key k)
	{
		return RSA.getBytesFromBigInteger(DecryptToBigInt(RSA.getBigIntegerFromBytes(tab), k));
		//return RSA.getBytesFromBigIntegerArray(Decrypt(RSA.getBigIntegerArrayFromBytes(tab),k));
	}
}
